﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebStudio.Business.Entity;
using WebStudio.Framework.Helper;
using System.Data;

namespace WebStudio.Business.Rule
{
    public class PhotoRule
    {
        #region 共有方法

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="condition">条件</param>
        /// <returns>列表</returns>
        public static List<PhotoEntity> GetPhotoList(string condition)
        {
            List<PhotoEntity> list = new List<PhotoEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM PhotoTB where {0}", condition)))
            {
                return FillData(dr);
            }
        }

        public static List<PhotoEntity> GetPhotoList(int top)
        {
            List<PhotoEntity> list = new List<PhotoEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT TOP {0} * FROM PhotoTB", top)))
            {
                return FillData(dr);
            }
        }

        public static List<PhotoEntity> GetDeepPhotoList(string condition)
        {
            List<PhotoEntity> list = new List<PhotoEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM PhotoTB where {0}", condition)))
            {
                return DeepFillData(dr);
            }
        }

        public static List<PhotoEntity> GetPhotoList(int pageIndex, int pageSize, int pageCount, int recordCount)
        {
            List<PhotoEntity> list = new List<PhotoEntity>();
            string strSql = string.Empty;
            if (pageIndex >= pageCount)
            {
                if (recordCount == 0)
                {
                    strSql = "select * from (select top " + pageSize + " * from (select top " + pageIndex * pageSize + " * FROM PhotoTB order by PhotoId desc) order by PhotoId) order by PhotoId desc";
                }
                else
                {
                    strSql = "select * from (select top " + (recordCount - (pageIndex - 1) * pageSize) + " * from (select top " + pageIndex * pageSize + " * FROM PhotoTB order by PhotoId desc) order by PhotoId) order by PhotoId desc";
                }
            }
            else
            {
                strSql = "select * from (select top " + pageSize + " * from (select top " + pageIndex * pageSize + "  * FROM PhotoTB order by PhotoId desc) order by PhotoId) order by PhotoId desc";
            }
            using (var dr = DBHelper.ExecuteReader(strSql))
            {
                return FillData(dr);
            }
        }

        public static int GetPhotoListCount()
        {
            return DBHelper.ExecuteIntScalar(string.Format("SELECT count(*) FROM PhotoTB"));
        }

        /// <summary>
        /// 获取信息实体
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static PhotoEntity GetPhotoEntity(string condition)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM PhotoTB WHERE {0}", condition)))
            {
                List<PhotoEntity> list = FillData(dr);
                return list == null || list.Count == 0 ? (new PhotoEntity()) : list[0];
            }
        }

        /// <summary>
        /// 根据标识获取信息实体重载一
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static PhotoEntity GetPhotoEntity(int photoId)
        {
            return GetPhotoEntity(string.Format("PhotoId = {0}", photoId));
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="te"></param>
        /// <returns></returns>
        public static bool SavePhotoInfor(PhotoEntity te)
        {
            int i = 0;
            if (te.PhotoId <= 0)
            {
                i = DBHelper.ExecuteNonQuery(string.Format("INSERT INTO PhotoTB (PhotoName,PhotoDescribe,AlbumId,CrtTime,PhotoUrl) VALUES('{0}','{1}',{2},'{3}','{4}')", te.PhotoName, te.PhotoDescribe, te.AlbumInfo.AlbumId, te.CrtTime, te.PhotoUrl));
            }
            else
            {
                i = DBHelper.ExecuteNonQuery(string.Format("UPDATE PhotoTB SET PhotoName ='{0}' ,PhotoUrl ='{1}',PhotoDescribe='{2}',CrtTime='{3}',AlbumId={4} where PhotoId={5}", te.PhotoName, te.PhotoUrl, te.PhotoDescribe, te.CrtTime, te.AlbumInfo.AlbumId, te.PhotoId));
            }
            return i > 0 ? true : false;
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool DeletePhotoInfor(int photoId)
        {
            return DBHelper.ExecuteNonQuery(string.Format("DELETE FROM PhotoTB WHERE PhotoId = {0}", photoId)) >= 0 ? true : false;
        }
        #endregion

        #region 私有方法

        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private static List<PhotoEntity> FillData(IDataReader dr)
        {
            List<PhotoEntity> list = new List<PhotoEntity>();
            PhotoEntity te;
            while (dr != null && dr.Read())
            {
                te = new PhotoEntity();
                te.PhotoId = ConvertHelper.IntParse(dr["PhotoId"]);
                te.PhotoName = ConvertHelper.StringParse(dr["PhotoName"]);
                te.PhotoDescribe = ConvertHelper.StringParse(dr["PhotoDescribe"]);
                te.AlbumInfo.AlbumId = ConvertHelper.IntParse(dr["AlbumId"]);
                te.CrtTime = ConvertHelper.DateTimeParse(dr["CrtTime"]);

                te.PhotoUrl = ConvertHelper.StringParse(dr["PhotoUrl"]);

                list.Add(te);
            }
            return list;
        }

        private static List<PhotoEntity> DeepFillData(IDataReader dr)
        {
            List<PhotoEntity> list = new List<PhotoEntity>();
            PhotoEntity te;
            while (dr != null && dr.Read())
            {
                te = new PhotoEntity();
                te.PhotoId = ConvertHelper.IntParse(dr["PhotoId"]);
                te.PhotoName = ConvertHelper.StringParse(dr["PhotoName"]);
                te.PhotoDescribe = ConvertHelper.StringParse(dr["PhotoDescribe"]);
                te.AlbumInfo.AlbumId = ConvertHelper.IntParse(dr["AlbumId"]);
                te.AlbumInfo.AlbumName = ConvertHelper.StringParse(dr["AlbumName"]);
                te.CrtTime = ConvertHelper.DateTimeParse(dr["CrtTime"]);

                te.PhotoUrl = ConvertHelper.StringParse(dr["PhotoUrl"]);

                list.Add(te);
            }
            return list;
        }

        #endregion
    }
}
